package com.servlet.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.servlet.dao.CarrierDao;
import com.servlet.service.ICarrierService;
import com.servlet.vo.bean.Carrier;

public class CarrierServiceImpl implements ICarrierService{

	private boolean flag = false;
	private CarrierDao carrierDao;
	
	public CarrierServiceImpl() throws SQLException{
		carrierDao = new CarrierDao();
	}
	
	/**
	 * 新增载体
	 * @param req
	 * @return
	 */
	public boolean save(HttpServletRequest req){
		
		String acc = req.getParameter("acc");
		String ca_name = req.getParameter("ca_name");
		Carrier obj = new Carrier();
		obj.setAcc_id(Long.parseLong(acc));
		obj.setCa_name(ca_name);
		obj.setAvailableBalance(BigDecimal.ZERO);
		flag = carrierDao.save(obj);
		return flag;
	}
	
	public List<Carrier> getObjByAccountId(Long accountId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("acc_id", accountId+"");
		
		return carrierDao.getListByProperties(params);
	}
	
	/**
	 * @param id 金融载体id
	 * @return  有则返回实体，无则返回null
	 */
	public Carrier getObjById(Long id) {
		return carrierDao.getObj(id);
	}
	
	/**
	 * 更新 金融载体的可用余额
	 */
	public boolean refreshAvailableBalance(Carrier obj) {
		return carrierDao.update(obj, true);
	}
	
}
